package com.iafme.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.iafme.config.EmailConfig;
import com.iafme.dto.req.*;
import com.iafme.dto.res.*;
import com.iafme.freemarker.TemplateConfiguration;
import com.iafme.model.*;
import com.iafme.model.Process;
import com.iafme.service.*;
import com.iafme.utils.EmailUtils;
import com.systemframework.exception.ResultException;
import com.systemframework.util.SearchUtl;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/11 09:52
 */
@RestController
@AllArgsConstructor
@SaIgnore
@RequestMapping("/web")
public class WebController {

    private final StudyService studyService;

    /**
     * 活动研究列表
     * @param studyReqDTO type 0活动 1研究
     * @return
     */
    @PostMapping(value = "/studyLists")
    public Page<StudyResDTO> studyLists(@RequestBody StudyReqDTO studyReqDTO){

        return studyService.lists(studyReqDTO);
    }

    @GetMapping(value = "/findStudyById/{id}")
    public StudyResDTO findStudyById(@PathVariable Integer id){


        return Convert.convert(StudyResDTO.class,studyService.getById(id));
    }


    private final CawService cawService;

    @PostMapping(value = "/cawLists")
    public Page<CawResDTO> cawLists(@RequestBody CawReqDTO cawReqDTO){

        return cawService.lists(cawReqDTO);
    }

    private final ProcessService processService;

    /**
     * 发展历程
     * @return
     */
    @GetMapping(value = "/processLists")
    public List<ProcessResDTO> lists(){
        LambdaQueryWrapper<Process> wrapper = Wrappers.lambdaQuery(Process.class).orderByDesc(Process::getYear);
        return Convert.toList(ProcessResDTO.class, processService.list(wrapper));
    }

    private final BeanSearcher beanSearcher;

    @PostMapping(value = "/newsList")
    public Page<NewsResDTO> newsResDTOList(@RequestBody NewsReqDTO newsReqDTO){
        Map<String, Object> build = MapUtils.builder().page(newsReqDTO.getPageNum()-1, newsReqDTO.getPageSize())
                 .build();
        return SearchUtl.searchDataList(newsReqDTO.getPageNum(), newsReqDTO.getPageSize(), beanSearcher.search(NewsResDTO.class,build));
    }

    private final NewsService newsService;

    @GetMapping(value = "/findNewsById/{id}")
    public NewsResDTO findNewsById(@PathVariable Integer id){
        News news = newsService.getById(id);
        return Convert.convert(NewsResDTO.class,news);
    }

    private final CarouselService carouselService;

    /**
     * 轮播图
     * @return
     */
    @GetMapping(value = "/carouselList")
    public List<CarouselResDTO> carouselResDTOList(){
        LambdaQueryWrapper<Carousel> eq = Wrappers.lambdaQuery(Carousel.class).eq(Carousel::getType, 0);
        return Convert.toList(CarouselResDTO.class,carouselService.list(eq));
    }

    /**
     * 合作伙伴
     * @return
     */
    @GetMapping(value = "/cooperateList")
    public List<CarouselResDTO> cooperateResDTOList(){
        LambdaQueryWrapper<Carousel> eq = Wrappers.lambdaQuery(Carousel.class).eq(Carousel::getType, 1);
        return Convert.toList(CarouselResDTO.class,carouselService.list(eq));
    }

    private final CollegesService collegesService;

    /**
     * 高校合作
     * @return
     */
    @GetMapping(value = "/collegesList")
    public List<CollegesResDTO> collegesList(){
        return Convert.toList(CollegesResDTO.class,collegesService.list());
    }

    private  final EnterpriseService enterpriseService;

    /**
     * 培训商合作
     * @return
     */
    @GetMapping(value = "/enterpriseList")
    public List<EnterpriseResDTO> enterpriseList(){
        return Convert.toList(EnterpriseResDTO.class,enterpriseService.list());
    }

    //国内委员
    private final ChinaService chinaService;

    @GetMapping(value = "/chinaList")
    public List<ChinaResDTO> chinaList(){
        return Convert.toList(ChinaResDTO.class,chinaService.list());
    }

    //国外委员
    private final StandardService standardService;

    @GetMapping(value = "/standardList")
    public List<StandardResDTO> standardList(){
        return Convert.toList(StandardResDTO.class,standardService.list());
    }

    //学会简介、核心价值观
    private final LearnService learnService;

    @GetMapping(value = "/learnInfo/{typeId}")
    public LearnResDTO learnResDTO(@PathVariable Integer typeId){
        return Convert.convert(LearnResDTO.class,learnService.getOne(Wrappers.lambdaQuery(Learn.class).eq(Learn::getType, typeId)));
    }




    private final EmailConfig emailConfig;

    @PostMapping(value = "/sendCode")
    public String sendCode(@RequestBody LoginReqDTO reqDTO){
        emailConfig.sendEmailCode(reqDTO.getEmail());
        return "邮箱发送成功,请注意查收";
    }

    /**
     * 职业认证
     * @param cawReqDTO caw type 0 cifa type 1
     * @return
     */
    @PostMapping(value = "/cawList")
    public Page<CawResDTO> cwaList(@RequestBody CawReqDTO cawReqDTO){
        return cawService.lists(cawReqDTO);
    }

    private final CawFileService cawFileService;

    @GetMapping(value = "/findCawById/{id}")
    public CawResDTO findCawById(@PathVariable Integer id){
        CawResDTO cawResDTO = Convert.convert(CawResDTO.class, cawService.getById(id));
        LambdaQueryWrapper<CawFile> eq = Wrappers.lambdaQuery(CawFile.class).eq(CawFile::getCawId, id);
        List<CawFile> list = cawFileService.list(eq);
        List<CawFileResDTO> cawFileResDTOList = Convert.toList(CawFileResDTO.class, list);
        cawResDTO.setCawFileList(cawFileResDTOList);

        return cawResDTO;
    }

    private final InformationService informationService;

    /**
     * 接收资讯发送邮件
     * @param requestDTO
     * @return
     */
    @PostMapping(value = "/saveInformation")
    public String saveInformation(@RequestBody InformationReqDTO requestDTO){
        String email = EmailUtils.email(requestDTO.getEmail());
        if(StrUtil.isEmpty(email)){
            throw new ResultException("请输入电子邮件");
        }
        LambdaQueryWrapper<Information> wrapper = Wrappers.lambdaQuery(Information.class).eq(Information::getEmail,email);
        Information information = informationService.getOne(wrapper);
        if(ObjectUtil.isEmpty(information)){
            information=new Information();
            information.setRecipient(requestDTO.getEmail());
            information.setEmail(email);
            information.setCreateTime(LocalDateTime.now());
            informationService.save(information);
        }
        return "提交成功";
    }

    /**
     * 证书查询
     * @return
     */
    @PostMapping(value = "/searchCertificateList")
    public List<SearchCertificateResDTO> searchCertificateList(@RequestBody SearchCertificateReqDTO reqDTO){
        Map<String, Object> build = MapUtils.builder().field(SearchCertificateResDTO::getName, reqDTO.getName()).op(Equal.class)
                .field(SearchCertificateResDTO::getCertificateNo, reqDTO.getCertificateNo()).op(Equal.class).build();

        return beanSearcher.searchAll(SearchCertificateResDTO.class,build);
    }

    @PostMapping(value = "/cawPage")
    public Page<CawResDTO> cawPage(@RequestBody CawReqDTO cawReqDTO){
        Page<Caw> page=new Page<>(cawReqDTO.getPageNum(),cawReqDTO.getPageSize());
        LambdaQueryWrapper<Caw> like = Wrappers.lambdaQuery(Caw.class).or().like(Caw::getName, cawReqDTO.getName())
                .or().like(Caw::getContext, cawReqDTO.getContext());
        return Convert.convert(new TypeReference<Page<CawResDTO>>() {
        },cawService.page(page,like));
    }

    @GetMapping(value = "/pay")
    public String pay(){

        return WxPayNotifyResponse.success("成功");
    }


}
